package com.escool.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.SgUsuarios;
import com.escool.model.SgUsuariosHasSgRoles;
import com.escool.model.TbAlumno;
import com.escool.model.TbExpediente;
import com.escool.model.TbResponsable;
import com.escool.service.AlumnoService;
import com.escool.service.ExpedienteService;
import com.escool.service.FindService;
import com.escool.service.PagadorService;
import com.escool.service.SgUsuarioService;

@Controller
public class AlumnoController {
	
	@Autowired
	ExpedienteService expedienteService;
	
	@Autowired
	AlumnoService alumnoService;
	
	@Autowired
	PagadorService pagadorService;
	
	@Autowired
	FindService findService;
	
	@Autowired
	SgUsuarioService userService;
	
	@RequestMapping(value = "showAlumno", method = RequestMethod.POST)
	public String listAlumno(@ModelAttribute("pagador") @Valid TbResponsable responsable, BindingResult result, Map<String, Object> map){
		try{
		TbAlumno alumno = new TbAlumno();
		alumno.setTbResponsable(responsable);
		map.put("alumno", alumno);
		map.put("parentescoList", findService.listTbParentesco());
		map.put("alumnoList", alumnoService.listByResponsable(responsable.getIdResponsable()));
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return "/alumno";
	}
	
	@RequestMapping(value = "showAlumnoDet")
	public String showGradosDet(@ModelAttribute("alumno") @Valid TbAlumno alumno,BindingResult result, Map<String, Object> map,HttpServletRequest request){
		
		try{
			String idResp = null;
			
			idResp = (String) request.getParameter("idResp");
			 if(idResp == null){
				 idResp = String.valueOf(alumno.getTbResponsable().getIdResponsable());
			 }
			 System.out.println(idResp);
			 alumno.setTbResponsable(null);
			 alumno.setTbResponsable(pagadorService.findById(Integer.parseInt(idResp)));
			 map.put("parentescoList", findService.listTbParentesco());
			 map.put("alumnoList", alumnoService.listByResponsable(Integer.parseInt(idResp)));
		}
		 catch(Exception ex){
			 ex.printStackTrace();
		 }
		return "/alumno";
	}
	
	@RequestMapping(value = "addAlumno", method = RequestMethod.POST)
	public String addAlumno(@ModelAttribute("alumno") @Valid TbAlumno alumno,BindingResult result){
		
			System.out.println(alumno.getTbResponsable().getIdResponsable());
			TbResponsable responsable = pagadorService.findById(alumno.getTbResponsable().getIdResponsable());
			alumno.setTbResponsable(responsable);
			System.out.println(alumno.getFechaNacAlumno());
			System.out.println(responsable);
			alumnoService.add(alumno);
			TbExpediente tbExpediente = new TbExpediente();
			tbExpediente.setTbAlumno(alumno);
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			String fechaString = sdf.format(date);
			tbExpediente.setFechaCreaExpediente(fechaString);
			tbExpediente.setCodigoAlumno(alumno.getCodigoAlumno());
			expedienteService.add(tbExpediente);
			//Crear usuario
			SgUsuarios user = new SgUsuarios();
			user.setNombreUsuario(alumno.getCodigoAlumno());
			user.setPassword(alumno.getCodigoAlumno());
			user.setNumeroEmpleado(alumno.getCodigoAlumno());
			userService.addUsuarios(user);
			//Enrolar usuario alumno
			SgUsuariosHasSgRoles role = new SgUsuariosHasSgRoles();
			role.setSgUsuarios(user);
			role.setSgRoles(userService.findRole(5));
			userService.addUsarioRol(role);
			System.out.println("Usuario alumno: " + user.getNombreUsuario() + " con rol: " + role.getSgRoles());
			System.out.println("Id alumno:" + alumno.getIdAlumno());
			System.out.println("Id alumno expediente:" + tbExpediente.getTbAlumno().getIdAlumno());
			return "redirect:/showAlumnoDet?idResp="+alumno.getTbResponsable().getIdResponsable();
		
	}
	
	@RequestMapping(value = "editAlumno", method = RequestMethod.POST)
	public String editAlumno(@ModelAttribute("alumno") @Valid TbAlumno alumno,BindingResult result){
		if(result.hasErrors()){
			alumnoService.edit(alumno);
			return "redirect:/showAlumnoDet?idResp="+alumno.getTbResponsable().getIdResponsable();
		}
		System.out.println(result.getFieldErrorCount() + " , " + result.getNestedPath());
		return "/index";
	}
	
	@RequestMapping(value = "deleteAlumno", method = RequestMethod.POST)
	public String deleteAlumno(@ModelAttribute("alumno") @Valid TbAlumno alumno,BindingResult result){
		if(!result.hasErrors()){
			alumnoService.delete(alumno.getIdAlumno());
			return "redirect:/showAlumnoDet?idResp="+alumno.getTbResponsable().getIdResponsable();
		}
		System.out.println(result.getFieldErrorCount() + " , " + result.getNestedPath());
		return "/index";
	}

	
	
	@RequestMapping(value = "showAlumnoParientes", method = RequestMethod.POST)
	public String showAlumnoParientes(@ModelAttribute("alumno") @Valid TbAlumno alumno, BindingResult result, Map<String, Object> map){
		try{
			map.put("alumno", alumno);
			map.put("listVwParentesco", findService.listVwParentesco(alumno.getCodigoAlumno()));
			return "/findParientesAlumno";
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return "/alumno";
	}
	
	@RequestMapping(value = "showExpedienteAlumno", method = RequestMethod.POST)
	public String deleteAlumno(Map<String, Object> map,@ModelAttribute("alumno") @Valid TbAlumno alumno,BindingResult result){		
		map.put("expediente", new TbExpediente());
		map.put("expedienteList", expedienteService.listAll(alumno.getCodigoAlumno()));
		return "expediente";		
	}
}
